<?php
/*
 * IWA assignment 2 - service.php 
 * Maximilian Einoeder
 * Mark Mensing
 */

// Define error reporting level
error_reporting(E_ALL ^ E_DEPRECATED ^ E_WARNING ^ E_NOTICE);

header('Content-Type: text/html; charset=utf-8');

// Eventful API configuration parameters
$app_key='58SW64N5rgq2VXQ7';
$keywords='test';
$domain = 'http://api.eventful.com';
$service = '/rest/events/search';

//Get the input text
$query = $_POST['textQuery'];

$keywords = array();

//Release a regex on the input, to retrieve the literals
preg_match_all('/"([^"]+)"/', $query, $keywords);


//For now print out the keywords
$count = count($keywords[0]);

$key = $keywords[1][0];
for($i = 1; $i < $count; $i++) {
    $key.='+'.$keywords[1][$i];
}

// OVERWRITE THIS
//$key = 'Amsterdam';

// create API call 
$url = $domain.$service.'?app_key='.$app_key.'&keywords='.$key;
$url = str_replace(" ","+",$url);

// output URL
echo "<h2>Called URL:</h2> ".$url;

// get return from Service
$result=file_get_contents($url);

// parse XML
$xmlresult = new SimpleXMLElement($result);

// Include RAP
define("RDFAPI_INCLUDE_DIR", "./api/");
include(RDFAPI_INCLUDE_DIR . "RDFAPI.php");

// Filename of an RDF document
// namespaces are defined within the file
$base="events.rdf";

// Create a new MemModel 
$model = ModelFactory::getDefaultModel();

// Load and parse document
$model->load($base);

// fetch web services results 
foreach($xmlresult->events->children() as $x) {
$node = new BlankNode($model,$x['id']);


// store XML results into Triplestore, according to lode namespace
$statement1 = new Statement($node,new Resource("rdf:ch_type"),new Resource("lode:Event"));
$statement2 = new Statement($node,new Resource("rdf:ch_label"),new Literal($x->title));
$statement3 = new Statement($node,new Resource("lode:atPlace"),new Literal($x->venue_name));
$statement32 = new Statement($node,new Resource("lode:atPlace"),new Literal($x->city_name));
$inspacenode = new BlankNode($model);
$statement40 = new Statement($node,new Resource("lode:inSpace"),$inspacenode);
$statement41 = new Statement($inspacenode,new Resource("rdf:type"),new Resource("geo:SpatialThing"));
$statement42 = new Statement($inspacenode,new Resource("geo:latitude"),new Literal($x->latitude));
$statement43 = new Statement($inspacenode,new Resource("geo:longitude"),new Literal($x->longitude));
$statement5 = new Statement($node,new Resource("lode:illustrate"),new Literal($x->url));
$statement6 = new Statement($node,new Resource("rdfs:label"),new Literal($x->title));
$statement7 = new Statement($node,new Resource("rdfs:comment"),new Literal($x->description));

// add statements to model
$model->add($statement1);
$model->add($statement2);
$model->add($statement3);
$model->add($statement32);
$model->add($statement40);
$model->add($statement41);
$model->add($statement42);
$model->add($statement43);
$model->add($statement5);
$model->add($statement6);
$model->add($statement5);
}


$query = htmlentities($query);
//print query
echo "<br /><h2>Your Query:</h2>".$query;

// bring SPARQL query to RDF store and print results
echo "<br /><h2>Your results:</h2>".$model->sparqlQuery(html_entity_decode($query), 'HTML');

// just for a test
//$query="PREFIX lode: <http://linkedevents.org/ontology/> SELECT ?a ?b ?c WHERE { ?a ?b \"Amsterdam\" }";
//echo "<br /><h2>Your results:</h2>".$model->sparqlQuery($query, 'HTML');

// print content of RDF store
echo "<br /><h2>Your full RDF-store:</h2>";
$model->writeAsHtmlTable();


// Save model to file
$model->saveAs("Output.rdf");
?>

